//package cn.wanda.bi.sunshine.platform.server.service.authorize;
//
//import java.util.Calendar;
//import java.util.List;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
//import org.springframework.stereotype.Service;
//
//import cn.wanda.bi.sunshine.platform.server.SunshineCode;
//import cn.wanda.bi.sunshine.platform.server.aspect.AccessTimes;
//import cn.wanda.bi.sunshine.platform.server.bean.KeyCommon;
//import cn.wanda.bi.sunshine.platform.server.controller.response.SysUserResponse;
//import cn.wanda.bi.sunshine.platform.server.controller.response.common.SimpleResponse;
//import cn.wanda.bi.sunshine.platform.server.security.bean.mapper.SysUserMapper;
//import cn.wanda.bi.sunshine.platform.server.security.bean.model.SysUser;
//import cn.wanda.bi.sunshine.platform.server.security.bean.model.SysUserExample;
//import cn.wanda.bi.sunshine.platform.server.utils.PasswordUtil;
//import cn.wanda.bi.sunshine.platform.server.utils.exception.SunshineException;
//
///**
// * Created by wangfupeng1
// */
//@Service
//public class SysUserService {
//
//    public static final Logger logger = LoggerFactory.getLogger(SysUserService.class);
//
//    @Autowired
//    private SysUserMapper sysUserMapper;
//
//    /**
//     * 用户登录校验
//     */
//    @Cacheable(value = "cache:sysuser", key = "'cache:sysuser:sysuserlogin:name:'+#name+':password:'+#password")
//    public SimpleResponse sysUserLogin(String name, String password) {
//        SysUserExample example = new SysUserExample();
//        example.createCriteria().andLoginNameEqualTo(name)
//                .andLoginPasswordEqualTo(PasswordUtil.encryptPasswordBASE64(password));
//        List<SysUser> sysUserList = sysUserMapper.selectByExample(example);
//        if (org.springframework.util.CollectionUtils.isEmpty(sysUserList)) {
//            throw new SunshineException(SunshineCode.USER_NAME_WRONG_OR_PASSWORD_WRONG);
//        }
//        SysUserResponse sysUserResponse = new SysUserResponse();
//        SysUser sysUser = new SysUser();
//        sysUser = sysUserList.get(0);
//        sysUser.setLoginPassword("");
//        sysUserResponse.setSysUser(sysUser);
//        sysUserResponse.setUserName(sysUser.getLoginName());
//        long timeNow = Calendar.getInstance().getTimeInMillis();
//        sysUserResponse.setLoginTime(timeNow);
//        sysUserResponse.setToken(PasswordUtil.MD5LowerCaseWithExtraKey(sysUser.getLoginName() + timeNow));
//        return SimpleResponse.success(sysUserResponse);
//    }
//
//    /**
//     * 查询特定userName的记录
//     */
//    @Cacheable(value = "cache:sysuser", key = "'cache:sysuser:userinfobyname:loginname:'+#loginName")
//    private SysUser selectUserInfoByName(String loginName) {
//        SysUserExample example = new SysUserExample();
//        example.createCriteria().andLoginNameEqualTo(loginName);
//        List<SysUser> sysUserList = sysUserMapper.selectByExample(example);
//        if (org.springframework.util.CollectionUtils.isEmpty(sysUserList)) {
//            return  null;
//        }
//        return sysUserList.get(0);
//    }
//
//    /**
//     * 插入数据库
//     */
//    public SimpleResponse sysUserInsert(String name) {
//        SysUser newSysUser;
//
//        newSysUser = selectUserInfoByName(name);
//        if( newSysUser == null) {
//        	newSysUser = new SysUser(); 
//            newSysUser.setLoginName(name);
//            newSysUser.setLoginPassword(PasswordUtil.encryptPasswordBASE64(KeyCommon.DEFAULT_USER_PASSWORD));
//            newSysUser.setRoleId(KeyCommon.DEFAULT_USER_ROLE_ID);
//            newSysUser.setUserName(name);
//            newSysUser.setUserSex(KeyCommon.DEFAULT_USER_SEX);
//            newSysUser.setMobile(KeyCommon.DEFAULT_USER_MOBILE);
//            newSysUser.setUserType(KeyCommon.DEFAULT_USER_TYPE);
//            newSysUser.setUserState(KeyCommon.DEFAULT_USER_STATE);
//            sysUserMapper.insert(newSysUser);
//        }
//
//        SysUserResponse sysUserResponse = new SysUserResponse();
//        newSysUser.setLoginPassword("");
//        sysUserResponse.setSysUser(newSysUser);
//        sysUserResponse.setUserName(newSysUser.getLoginName());
//
//        long timeNow = Calendar.getInstance().getTimeInMillis();
//        sysUserResponse.setLoginTime(timeNow);
//        sysUserResponse.setToken(PasswordUtil.MD5LowerCaseWithExtraKey(newSysUser.getLoginName() + timeNow));
//        return SimpleResponse.success(sysUserResponse);
//    }
//
//}
